﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HA.ModFlow.Domain.FileLayer;

using HAUtil;

using System.Runtime.Serialization.Formatters.Binary;

using System.Runtime.Serialization;
using HA.ModFlow.Infrastructure;


namespace HA.ModFlow.Domain.DomainLayer
{

    /// <summary>
    /// 本类本设计用于存储所有的modflow数据
    /// </summary>
    [Serializable]
    public class ModFlowData
    {

        public ModFlowData(DefaultParamForModFlow param,GridData gd,Units units)
        {

            this._gridData = gd;

            _modelOption = new ModelOption(param, units);


            //时间离散部分
            _stressPeriods = new StressPeriodCollection();





            //_innerData = new MFInner();



            //this._innerData.OnResetGrid(this._gridData);

            //foreach (InnerLayer layer in _innerData.Layers)
            //{
            //    for (int i = 0; i < gd.RowsCount; i++)
            //        for (int j = 0; j < gd.ColsCount; j++)
            //        {
            //            layer.LpfLayer.Ss[i, j] = _modelOption.DefaultParam.Ss;
            //            layer.LpfLayer.Sy[i, j] = _modelOption.DefaultParam.Sy;
            //        }
            //}



            ///

            this._flowData = new InnerFlowV2(this._gridData);



            this._hfbData = new HFBData();

            //解法包部分

            _sipSolver = new SIPSolver();

            _pcgSolver = new PCGSolver();

            _dESolver = new DESolver();

            //输出控制
            _outputCon = new OutputControl();



            //结果保存类
            this._mfResult = new OutputResult();


            ///源汇部分
            ///
            this._sourceSinks = new SourceSink(this._gridData.RowsCount,_gridData.ColsCount,_gridData.LayersCount,_stressPeriods.InnerList.Count);



            //订阅对应的事件


            this.ReserveInnerEvents();


            //初始化一个应力期

            StressPeriod sp = new StressPeriod();

            

            this._stressPeriods.AddStressPeriod(sp);

           
 
        }

        private double _HNOFLO = -999.0;
        /// <summary>
        /// 无效单元水头
        /// </summary>
        public double HNOFLO
        {
            get { return _HNOFLO; }
            set { _HNOFLO = value; }
        }

        private string _title1 = "Modflow_1";
        /// <summary>
        /// 标题1
        /// </summary>
        public string Title1
        {
            get { return _title1; }
            set { _title1 = value; }
        }
        private string _title2 = "Modflow_2";
        /// <summary>
        /// 标题1
        /// </summary>
        public string Title2
        {
            get { return _title2; }
            set { _title2 = value; }
        }
        protected void ReserveInnerEvents()
        {
             this._stressPeriods.StressPeriodChanged += new EventHandler<StressPeriodChangedEventArgs>(_stressPeriods_StressPeriodChanged);

             this._gridData.WaterGrid.WaterGridModified += new EventHandler<WaterGridModifiedEventArgs>(WaterGrid_WaterGridModified);


        }

        void WaterGrid_WaterGridModified(object sender, WaterGridModifiedEventArgs e)
        {
            if (e.Type==WaterGridModifiedType.IBOUND)
            {
                this._flowData.OnIboundsChanged(_gridData.WaterGrid.GetIBounds());
                
            }
        }



        void _stressPeriods_StressPeriodChanged(object sender, StressPeriodChangedEventArgs e)
        {
            this._sourceSinks.Wells._stressPeriods_StressPeriodChanged(sender, e);
           
            this._sourceSinks.Drains._stressPeriods_StressPeriodChanged(sender,e);

            this._sourceSinks.SpecifiedHeads._stressPeriods_StressPeriodChanged(sender,e);

            this._sourceSinks.Rivers._stressPeriods_StressPeriodChanged(sender,e);

            this._sourceSinks.GeneralHeads._stressPeriods_StressPeriodChanged(sender,e);





            this._sourceSinks.Recharges._stressPeriods_StressPeriodChanged(sender, e);

            this._sourceSinks.Evts._stressPeriods_StressPeriodChanged(sender,e);


            this._sourceSinks.UzfCollection._stressPeriods_StressPeriodChanged(sender ,e );


            ///这里用于更新是否含有瞬态应力期

            this._flowData.OnSetHaveStressPeriod(this._stressPeriods.IsHaveTransient);




        }



     



      

        #region 事件模型

        [field:NonSerialized]
        public event DefaultHandler GridReseted;

        

        #endregion



        #region 数据存储区

        StressPeriodCollection _stressPeriods;

        /// <summary>
        /// 应力期对应的设置
        /// </summary>
        public StressPeriodCollection StressPeriods
        {
            get { return _stressPeriods; }
           
        }




        ModelOption _modelOption;

        /// <summary>
        /// 模型运算相关参数
        /// </summary>
        public ModelOption ModelOption
        {
            get { return _modelOption; }
           
        }

        OutputControl _outputCon;

        /// <summary>
        /// 输出控制
        /// </summary>
        public OutputControl OutputCon
        {
            get { return _outputCon; }
           
        }

        GridData _gridData;

        /// <summary>
        /// 设置的网格
        /// </summary>
        public GridData GridData
        {
            get { return _gridData; }
            set {
                
                _gridData = value;

                this.OnSetGridData();
            }
        }


        protected void OnSetGridData()
        {

            //这里首先要更新自身的数据,内部的数据变更不通过事件进行，直接调去对应的代码


            //这里需要重新赋值所有源汇属性等数据，
            //根本上应该重建modlfow系统，除了流动和解法包，其它都将被重建





            this._flowData.OnResetGrid(this._gridData);

            //this._sourceSinks = new SourceSink();
            //结果保存类
            this._mfResult = new OutputResult();


            ///源汇部分
            ///
            this._sourceSinks.OnGridReset(this._gridData.RowsCount, _gridData.ColsCount, _gridData.LayersCount);




            if (GridReseted!=null)
            {
                this.GridReseted();
                
            }

        }

        //流动包部分

        //已经被废弃
        //MFInner _innerData;

        //public MFInner InnerData
        //{
        //    get { return _innerData; }
        //    set 
        //    {
        //        _innerData = value;
             
        //    }
            
        //}


        InnerFlowV2 _flowData;

        public InnerFlowV2 FlowData
        {
            get { return _flowData; }
            set { _flowData = value; }
        }


        HFBData _hfbData;

        public HFBData HfbData
        {
            get { return _hfbData; }
          
        }


        //protected void OnSetIsHavePeriod()
        //{
        //    _innerData.OnSetHaveStressPeriod(this._stressPeriods.IsHaveTransient);
        //}



        //解算包部分


        SIPSolver _sipSolver;

        public SIPSolver SipSolver
        {
            get { return _sipSolver; }
            set { _sipSolver = value; }
        }

        PCGSolver _pcgSolver;

        public PCGSolver PcgSolver
        {
            get { return _pcgSolver; }
            set { _pcgSolver = value; }
        }

        DESolver _dESolver;

        public DESolver DESolver
        {
            get { return _dESolver; }
            set { _dESolver = value; }
        }
        

       

       
        //下面是源汇项相关的一些内容

        SourceSink _sourceSinks;

        public SourceSink SourceSinks
        {
            get { return _sourceSinks; }
            
        }


      


        //最重要的结果数据存储

        [NonSerialized]
        OutputResult _mfResult;

        public OutputResult MfResult
        {
            get { return _mfResult; }
          
        }
       
       

        #endregion


        #region  方法部分




        /// <summary>
        /// 输出对应文件到指定的路径,主要用于运行前
        /// </summary>
        /// <param name="path">提供的路径地址比如c:\test2\solution</param>
        public void WriteFiles(string path)
        {
            ModFlowFiles files = new ModFlowFiles();

            ///循环所有的层
            ///为基础包提供数据
            for (int i = 0; i < this._gridData.LayersCount ; i++)
            {

                files.BasicPackage.IBOUND.Add(this._gridData.WaterGrid[i].IBound);
                files.BasicPackage.STRT.Add(this._gridData.WaterGrid[i].InitHead);
                
            }

            files.BasicPackage.HNOFLO = this.HNOFLO;
            //为IDS包提供数据支持


            files.DiscretizationPackage.NLAY = (uint)this._gridData.LayersCount;
            files.DiscretizationPackage.NROW = (uint)this._gridData.RowsCount;
            files.DiscretizationPackage.NCOL = (uint)this._gridData.ColsCount;
            files.DiscretizationPackage.NPER = (uint)this._stressPeriods.InnerList.Count;
            switch (this.ModelOption.Units.Time)
            {
                case TIME.SECOND:
                    files.DiscretizationPackage.ITMUNI = ITMUNI.seconds;
                    break;
                case TIME.MINUTE:
                    files.DiscretizationPackage.ITMUNI = ITMUNI.minutes;
                    break;
                case TIME.HOUR:
                    files.DiscretizationPackage.ITMUNI = ITMUNI.hours;
                    break;
                case TIME.DAY:
                    files.DiscretizationPackage.ITMUNI = ITMUNI.days;
                    break;
                case TIME.YEAR:
                    files.DiscretizationPackage.ITMUNI = ITMUNI.years;
                    break;
            }
            switch (this.ModelOption.Units.Length)
            {
                case LENGTH.FT:
                    files.DiscretizationPackage.ILUNI = ILUNI.feet;
                    break;
                case LENGTH.M:
                    files.DiscretizationPackage.ILUNI = ILUNI.meters;
                    break;
                case LENGTH.CM:
                    files.DiscretizationPackage.ILUNI = ILUNI.centimeters;
                    break;
            }

            double[] delr=new double[this._gridData.ColsCount];

            for (int i = 0; i < this._gridData.ColCoords.Length-1; i++)
            {
                delr[i] = this._gridData.ColCoords[i + 1] - this._gridData.ColCoords[i];
                
            }


            double[] delc = new double[this._gridData.RowsCount];

            for (int j = 0; j < this._gridData.RowCoords.Length-1; j++)
            {
                delc[j]=this._gridData.RowCoords[j+1]-this._gridData.RowCoords[j];
            }


            files.DiscretizationPackage.DELC = delc;
            files.DiscretizationPackage.DELR = delr;

            GridLayer gl;

            int [] layercbd=new int[ this._gridData.LayersCount];

            for (int i = 0; i < this._gridData.LayersCount; i++)
            {
                gl=this._gridData.WaterGrid[i];

                if (gl.LayerType == LAYERTYPE.NOCONFINED)
                {
                    layercbd[i] = 0;
                    files.DiscretizationPackage.BOTM.Add(this._gridData.Elevations[i+1]);

                }
                else
                {
                    layercbd[i] = 1;

                    files.DiscretizationPackage.BOTM.Add(this._gridData.Elevations[i + 1]);

                    files.DiscretizationPackage.BOTM.Add(gl.ConfinedBedElevation);

                }

            }

            files.DiscretizationPackage.LAYCBD = layercbd;
            files.DiscretizationPackage.Top = this._gridData.Elevations[0];


            foreach (StressPeriod period in this._stressPeriods.InnerList)
            {
                files.DiscretizationPackage.Steps.Add(new DisStep(period.PERLEN,period.NSTP,period.TSMULT,period.IsSteadyState));
            }






            ///流动包
            if (this.ModelOption.InnerPack == INNERPACK.LPF)
            {
                //PLayerPropertyFlow plpf = this._innerData.ToLPFPack();

                //plpf.IsHaveTransient = this._stressPeriods.IsHaveTransient;

                //int i = 0;
                //foreach (int item in files.DiscretizationPackage.LAYCBD)
                //{
                //    plpf.LAYCBD[i] = item;
                //    i++;
                //}



                PLayerPropertyFlow plpf = this._flowData.LpfData.ToLPFPack();

                int i = 0;
                foreach (int item in files.DiscretizationPackage.LAYCBD)
                {
                    plpf.LAYCBD[i] = item;
                    i++;
                }

                files.Packages.Add(plpf);



            }

            else
            {
                //PBlockCenteredFlow pbcf = this._innerData.ToBCFPack();

                //pbcf.IsHaveTransient = this._stressPeriods.IsHaveTransient;

                //files.Packages.Add(pbcf);



                PBlockCenteredFlow pbcf = this._flowData.BcfData.ToBCFPack();

               

                files.Packages.Add(pbcf);


            }

            ///解法包
            if (this.ModelOption.SolverPack == SOLVERPACK.SIP)
            {
                
                PStronglyImplicitProcedure sip = _sipSolver.ToSipPack();

                files.Packages.Add(sip);

            }
            else if (this.ModelOption.SolverPack == SOLVERPACK.PCG)
            {

                PPreconditionedConjugateGradient pcg = _pcgSolver.ToPack();

                files.Packages.Add(pcg);

            }
            else
            {
                PDirectSolver de = _dESolver.ToPack();

                files.Packages.Add(de);
            }

            ///源汇项,点状源汇

            if (_sourceSinks.Wells.Enabled)
            {
                PWell pw = _sourceSinks.Wells.ToWellPack();

                files.Packages.Add(pw);
            }




            if (this._sourceSinks.Rivers.Enabled)
            {
                PRiver river = this._sourceSinks.Rivers.ToRiverPack();

                files.Packages.Add(river);
                
            }

            if (this._sourceSinks.Drains.Enabled)
            {
                PDrain drain = this._sourceSinks.Drains.ToDrainPack();

                files.Packages.Add(drain);
                
            }
          

            if (this._sourceSinks.GeneralHeads.Enabled)
            {
                PGeneralHeadBoundary ghead = this._sourceSinks.GeneralHeads.ToGHBPack();

                files.Packages.Add(ghead);
                
            }


            if (this._sourceSinks.SpecifiedHeads.Enabled)
            {
                PSpecifiedHead phead = this._sourceSinks.SpecifiedHeads.ToSpecifiedHeadPack();

                files.Packages.Add(phead);
                
            }

            //源汇项之面状源汇
            if (this._sourceSinks.Recharges.Enabled)
            {
                PRecharge pRecharge = this._sourceSinks.Recharges.ToRechargePack();

                files.Packages.Add(pRecharge);
            }
            if (this.SourceSinks .UzfCollection .Enabled )
            {
                UzfPackage   uzfPackage= this.SourceSinks.UzfCollection.ToUZFPackage();

                files.Packages.Add(uzfPackage);
            }

            if (this._sourceSinks.Evts.Enabled)
            {
                PEvapotranspiration  pEvt = this._sourceSinks.Evts.ToEVTPack();

                files.Packages.Add(pEvt);
            }



            //hfb
            //if (this.HfbData.Enabled)
            //{
            //    PHorizontalFlowBarrier pHFB = this.HfbData.ToPack();
            //    files.Packages.Add(pHFB);                
            //}


            //输出控制
            POutputControl poc = this._outputCon.ToPack(this._stressPeriods);
            files.Packages.Add(poc);

           files.WriteFiles(path);
 
        }


        public void DeleteModflowFiles(string path)
        {

            ModFlowFiles.DeleteModflowFiles(path );

        
        }



        /// <summary>
        /// 读取本次运行结果文件
        /// </summary>
        public void ReadOutputResult(string path)
        {

            if (this._gridData != null)
            {
                List<int> stressPeriodNSTP = new List<int>();
                for (int i = 0; i < this._stressPeriods.InnerList.Count; i++)
                {
                    stressPeriodNSTP.Add(this._stressPeriods.InnerList[i].NSTP);
                }

                //this._mfResult.LoadData(path, this._gridData.RowsCount, this._gridData.ColsCount, this._gridData.LayersCount, 
                //    this._stressPeriods.InnerList.Count,stressPeriodNSTP, 
                //    this._outputCon,
                //     this._gridData.WaterGrid.GetIBounds()
                //    );


                if (this._mfResult == null)
                {
                    this._mfResult = new OutputResult();
                }



                //说明：首先读取IBOund文件，并存储（结果为多个应力期多个时间步长的数据）。然后读取IHead和DD降深数据，并根据应力期时间步长信息
                //将value值和对应的IBound值一同存储到CellCenterDataSet3D中。



                //读取IBound文件并存储（以前的逻辑是，读取IBound文件  并将数据反向更新到主程序中的IBound中）

                this._mfResult.LoadIBoundData(path, this._gridData.RowsCount, this._gridData.ColsCount, this._gridData.LayersCount,
                    this._stressPeriods.InnerList.Count, stressPeriodNSTP,
                    this._outputCon, this._gridData);



                double invalidValue = -888.0;
                if (_flowData != null)
                {
                    invalidValue = _flowData.HDRY;

                }

                //读取IHead水头 和IDDNUN 降深文件数据 
                this._mfResult.LoadData(path, this._gridData.WaterGrid.GetIBounds(), invalidValue);





                //好麻烦 有一个问题是由于dos运行完毕后，缺乏运行正确和计算正确性验算机制，可能存在计算出问题的情况
                //计算出问题的时候，各应力期IBound和各应力期IHead等数据个数不一致，此时为避免一些意外的处理，使用主程序的IBound

                bool isUseStressStepIBound = true;

                List<List<int[,]>> stressStpIBound = new List<List<int[,]>>();

                #region  判断读取IBound的数据和IHead的数据等是否一致 如果一致则使用读取IBound，如果不一致则使用主程序的IBound

                if (this._mfResult != null)
                {
                    if (this.MfResult.StressPeriodIBound != null && this.MfResult.CellCenterIHeadDataSet3d != null)
                    {
                        if (MfResult.StressPeriodIBound.Count != MfResult.CellCenterIHeadDataSet3d.Count)
                        {
                            //从文件中读取的IBound数据和IHead数据的各应力期的数据个数不一致 原因可能是项目计算出错

                            isUseStressStepIBound = false;
                        }
                        else
                        {


                            //将MfResult.StressPeriodIBound从形式List<int[, ,]> 转换成形式 List<List<int[,]>> 

                            stressStpIBound = this._mfResult.GetStressStepIBound();

                            if (stressStpIBound.Count > 0)
                            {
                            }
                            else 
                            {
                                isUseStressStepIBound = false;
                            }


                        }

                    }
                    else
                    {
                        isUseStressStepIBound = false;
                    }

                }
                else
                {
                    isUseStressStepIBound = false;

                    this._mfResult = new OutputResult();
                }

                #endregion

                if (isUseStressStepIBound)
                {
                    //使用读取的各应力期的IBound

                    #region

                    List<HA.ModFlow.Domain.DomainLayer.BudData> datas = new List<BudData>();

                    string filePath = path + "ccf\\";

                    //井
                    List<BudFile> data = new List<BudFile>();
                    if (this._sourceSinks.Wells.Enabled == true)
                    {
                        if (System.IO.File.Exists(filePath + "wel.bud") == true)
                        {
                            data = BudReader.ReadBudFile(filePath + "wel.bud", stressStpIBound, "wel");

                            if (data != null)
                            {
                                BudData well = new BudData(data, "wel");
                                well.Name = "WELL";

                                datas.Add(well);
                            }

                        }
                        else
                        {

                        }
                    }

                    //河流         
                    data = new List<BudFile>();
                    if (this._sourceSinks.Rivers.Enabled == true)
                    {
                        if (System.IO.File.Exists(filePath + "riv.bud") == true)
                        {
                            data = BudReader.ReadBudFile(filePath + "riv.bud", stressStpIBound, "riv");
                            if (data != null)
                            {
                                BudData river = new BudData(data, "riv");
                                river.Name = "RIVER";

                                datas.Add(river);
                            }

                        }
                        else
                        {
                        }
                    }

                    //drain
                    data = new List<BudFile>();
                    if (this._sourceSinks.Drains.Enabled == true)
                    {
                        if (System.IO.File.Exists(filePath + "drn.bud") == true)
                        {
                            data = BudReader.ReadBudFile(filePath + "drn.bud", stressStpIBound, "drn");
                            if (data != null)
                            {
                                BudData drain = new BudData(data, "drn");
                                drain.Name = "DRAIN";

                                datas.Add(drain);
                            }

                        }
                        else
                        {
                        }
                    }

                    //PGeneralHeadBoundary
                    data = new List<BudFile>();
                    if (this._sourceSinks.GeneralHeads.Enabled == true)
                    {
                        if (System.IO.File.Exists(filePath + "ghb.bud") == true)
                        {
                            data = BudReader.ReadBudFile(filePath + "ghb.bud", stressStpIBound, "ghb");
                            if (data != null)
                            {
                                BudData GeneralHeadBoundary = new BudData(data, "ghb");
                                GeneralHeadBoundary.Name = "PGeneralHeadBoundary";

                                datas.Add(GeneralHeadBoundary);
                            }

                        }
                        else
                        {
                        }
                    }

                    //PRecharge
                    data = new List<BudFile>();
                    if (this._sourceSinks.Recharges.Enabled == true)
                    {
                        if (System.IO.File.Exists(filePath + "rch.bud") == true)
                        {
                            data = BudReader.ReadBudFile(filePath + "rch.bud", stressStpIBound, "rch");
                            if (data != null)
                            {
                                BudData Recharge = new BudData(data, "rch");
                                Recharge.Name = "PRecharge";

                                datas.Add(Recharge);
                            }

                        }
                        else
                        {
                        }
                    }

                    //PEvapotranspiration
                    data = new List<BudFile>();
                    if (this._sourceSinks.Evts.Enabled == true)
                    {
                        if (System.IO.File.Exists(filePath + "evt.bud") == true)
                        {
                            data = BudReader.ReadBudFile(filePath + "evt.bud", stressStpIBound, "evt");
                            if (data != null)
                            {
                                BudData Evapotranspiration = new BudData(data, "evt");
                                Evapotranspiration.Name = "PEvapotranspiration";

                                datas.Add(Evapotranspiration);
                            }
                        }
                        else
                        {
                        }
                    }

                    //PLayerPropertyFlow  PBlockCenteredFlow
                    data = new List<BudFile>();
                    if (this._modelOption.InnerPack == INNERPACK.LPF)
                    {
                        if (System.IO.File.Exists(filePath + "lpf.bud") == true)
                        {
                            data = BudReader.ReadBudFile(filePath + "lpf.bud", stressStpIBound, "lpf");
                            if (data != null)
                            {
                                BudData LayerPropertyFlow = new BudData(data, "lpf");
                                LayerPropertyFlow.Name = "PLayerPropertyFlow";

                                datas.Add(LayerPropertyFlow);
                            }
                        }
                        else
                        {
                        }
                    }
                    else
                    {
                        if (System.IO.File.Exists(filePath + "bcf.bud") == true)
                        {
                            data = BudReader.ReadBudFile(filePath + "bcf.bud", stressStpIBound, "bcf");
                            if (data != null)
                            {
                                BudData BlockCenteredFlow = new BudData(data, "bcf");
                                BlockCenteredFlow.Name = "PBlockCenteredFlow";

                                datas.Add(BlockCenteredFlow);
                            }

                        }
                        else
                        {
                        }
                    }

#endregion

                    //存储对应的值

                    this._mfResult.CcfDataSet = datas;




                }
                else
                {
                    //使用主程序的IBound数据


                    # region 刘加，读取bud文件

                    List<HA.ModFlow.Domain.DomainLayer.BudData> datas = new List<BudData>();

                    string filePath = path + "ccf\\";

                    //井
                    List<BudFile> data = new List<BudFile>();
                    if (this._sourceSinks.Wells.Enabled == true)
                    {
                        if (System.IO.File.Exists(filePath + "wel.bud") == true)
                        {
                            data = BudReader.ReadBudFile(filePath + "wel.bud", this._gridData.WaterGrid.GetIBounds(), "wel");

                            if (data != null)
                            {
                                BudData well = new BudData(data, "wel");
                                well.Name = "WELL";

                                datas.Add(well);
                            }

                        }
                        else
                        {

                        }
                    }

                    //河流         
                    data = new List<BudFile>();
                    if (this._sourceSinks.Rivers.Enabled == true)
                    {
                        if (System.IO.File.Exists(filePath + "riv.bud") == true)
                        {
                            data = BudReader.ReadBudFile(filePath + "riv.bud", this._gridData.WaterGrid.GetIBounds(), "riv");
                            if (data != null)
                            {
                                BudData river = new BudData(data, "riv");
                                river.Name = "RIVER";

                                datas.Add(river);
                            }

                        }
                        else
                        {
                        }
                    }

                    //drain
                    data = new List<BudFile>();
                    if (this._sourceSinks.Drains.Enabled == true)
                    {
                        if (System.IO.File.Exists(filePath + "drn.bud") == true)
                        {
                            data = BudReader.ReadBudFile(filePath + "drn.bud", this._gridData.WaterGrid.GetIBounds(), "drn");
                            if (data != null)
                            {
                                BudData drain = new BudData(data, "drn");
                                drain.Name = "DRAIN";

                                datas.Add(drain);
                            }

                        }
                        else
                        {
                        }
                    }

                    //PGeneralHeadBoundary
                    data = new List<BudFile>();
                    if (this._sourceSinks.GeneralHeads.Enabled == true)
                    {
                        if (System.IO.File.Exists(filePath + "ghb.bud") == true)
                        {
                            data = BudReader.ReadBudFile(filePath + "ghb.bud", this._gridData.WaterGrid.GetIBounds(), "ghb");
                            if (data != null)
                            {
                                BudData GeneralHeadBoundary = new BudData(data, "ghb");
                                GeneralHeadBoundary.Name = "PGeneralHeadBoundary";

                                datas.Add(GeneralHeadBoundary);
                            }

                        }
                        else
                        {
                        }
                    }

                    //PRecharge
                    data = new List<BudFile>();
                    if (this._sourceSinks.Recharges.Enabled == true)
                    {
                        if (System.IO.File.Exists(filePath + "rch.bud") == true)
                        {
                            data = BudReader.ReadBudFile(filePath + "rch.bud", this._gridData.WaterGrid.GetIBounds(), "rch");
                            if (data != null)
                            {
                                BudData Recharge = new BudData(data, "rch");
                                Recharge.Name = "PRecharge";

                                datas.Add(Recharge);
                            }

                        }
                        else
                        {
                        }
                    }

                    //PEvapotranspiration
                    data = new List<BudFile>();
                    if (this._sourceSinks.Evts.Enabled == true)
                    {
                        if (System.IO.File.Exists(filePath + "evt.bud") == true)
                        {
                            data = BudReader.ReadBudFile(filePath + "evt.bud", this._gridData.WaterGrid.GetIBounds(), "evt");
                            if (data != null)
                            {
                                BudData Evapotranspiration = new BudData(data, "evt");
                                Evapotranspiration.Name = "PEvapotranspiration";

                                datas.Add(Evapotranspiration);
                            }
                        }
                        else
                        {
                        }
                    }

                    //PLayerPropertyFlow  PBlockCenteredFlow
                    data = new List<BudFile>();
                    if (this._modelOption.InnerPack == INNERPACK.LPF)
                    {
                        if (System.IO.File.Exists(filePath + "lpf.bud") == true)
                        {
                            data = BudReader.ReadBudFile(filePath + "lpf.bud", this._gridData.WaterGrid.GetIBounds(), "lpf");
                            if (data != null)
                            {
                                BudData LayerPropertyFlow = new BudData(data, "lpf");
                                LayerPropertyFlow.Name = "PLayerPropertyFlow";

                                datas.Add(LayerPropertyFlow);
                            }
                        }
                        else
                        {
                        }
                    }
                    else
                    {
                        if (System.IO.File.Exists(filePath + "bcf.bud") == true)
                        {
                            data = BudReader.ReadBudFile(filePath + "bcf.bud", this._gridData.WaterGrid.GetIBounds(), "bcf");
                            if (data != null)
                            {
                                BudData BlockCenteredFlow = new BudData(data, "bcf");
                                BlockCenteredFlow.Name = "PBlockCenteredFlow";

                                datas.Add(BlockCenteredFlow);
                            }

                        }
                        else
                        {
                        }
                    }


                    # endregion

                    //存储对应的值

                    this._mfResult.CcfDataSet = datas;



                }

            }


        }

       
        #endregion


        #region 反序体制

        [OnDeserialized]
        public void OnDeserialized(StreamingContext context)
        {


            this.ReserveInnerEvents();

            if (this.MfResult ==null )
            {
                this._mfResult = new OutputResult();
            }

        }

        //[OnSerializing]
        //public void OnSerializing(StreamingContext context)
        //{




        //}

        #endregion


    }
}
